﻿using System;

namespace Finance.Data.Entities
{
	public interface IDocumentBase : IDataBaseObject
	{
		string Description { get; set; }
		DateTime DocDate { get; set; }
		decimal Amount { get; set; }
		string SubTypeName { get; set; }
		DocumentType DocumentType { get; set; }
		string ContractorName { get; }
		IContractor Contractor { get; set; }
		Guid ContractorId { get; set; }
	}
	
	internal class DocumentBase : DataBaseObject, IDocumentBase
	{
		private readonly IContractors _contractors;
		private IContractor _contractor;

		public DocumentBase(DataBaseObjectCollection parentCollection, IContractors contractors)
			: base(parentCollection)
		{
			_contractors = contractors;
		}

		public string Description { get; set; }
		public IContractor Contractor
		{
			get
			{
				if (_contractor == null && !ContractorId.Equals(Guid.Empty))
					_contractor = _contractors.GetItem(ContractorId);
				return _contractor;
			}
			set
			{
				_contractor = value;
				ContractorId = _contractor != null ? _contractor.Id : Guid.Empty;
			}
		}

		public Guid ContractorId { get; set; }

		public DateTime DocDate { get; set; }
		public decimal Amount { get; set; }
		public string SubTypeName { get; set; }
		public DocumentType DocumentType { get; set; }

		public override string ToString()
		{
			return DocDate.ToShortDateString() + ":"+Amount + "-" + " " + SubTypeName + " [" + Description + "]";
		}

		public string ContractorName
		{
			get
			{
				return Contractor == null ? string.Empty : Contractor.Name;
			}
		}


	}
}
